/*
 * @Author: Zhengxuan Ye
 * @Date: 2025-05-06 21:29:07
 * @Last Modified by: Zhengxuan Ye
 * @Last Modified time: 2025-05-13 10:52:58
 */

import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

// 定义路由守卫
const requireAuth = (to, from, next) => {
  const token = localStorage.getItem('token')
  if (token) {
    // 如果路由是管理后台，检查是否是管理员
    if (to.path === '/admin') {
      const userStore = useUserStore()
      if (!userStore.isAdmin) {
        next('/admin-login')
        return
      }
    }
    next()
  } else {
    next({ name: 'login', query: { redirect: to.fullPath } })
  }
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/HomeView.vue'),
      meta: { title: '首页 - 中国非物质文化遗产' },
    },
    {
      path: '/heritage-list',
      name: 'heritage-list',
      component: () => import('../views/HeritageListView.vue'),
      meta: { title: '遗产名录 - 中国非物质文化遗产' },
    },
    {
      path: '/heritage/:id',
      name: 'heritage-detail',
      component: () => import('../views/HeritageDetailView.vue'),
      meta: { title: '遗产详情 - 中国非物质文化遗产' },
    },
    {
      path: '/featured/jing-tai-lan',
      name: 'jing-tai-lan',
      component: () => import('../views/featured/JingTaiLanView.vue'),
      meta: { title: '景泰蓝制作技艺 - 中国非物质文化遗产' },
    },
    {
      path: '/featured/su-zhou-embroidery',
      name: 'su-zhou-embroidery',
      component: () => import('../views/featured/SuZhouEmbroideryView.vue'),
      meta: { title: '苏州刺绣 - 中国非物质文化遗产' },
    },
    {
      path: '/featured/peking-opera',
      name: 'peking-opera',
      component: () => import('../views/featured/PekingOperaView.vue'),
      meta: { title: '京剧 - 中国非物质文化遗产' },
    },
    {
      path: '/featured/huang-mei-opera',
      name: 'huang-mei-opera',
      component: () => import('../views/featured/HuangMeiOperaView.vue'),
      meta: { title: '黄梅戏 - 中国非物质文化遗产' },
    },
    {
      path: '/protection',
      name: 'protection',
      component: () => import('../views/ProtectionView.vue'),
      meta: { title: '保护工作 - 中国非物质文化遗产' },
    },
    {
      path: '/exhibition',
      name: 'exhibition',
      component: () => import('../views/ExhibitionView.vue'),
      meta: { title: '展示与传承 - 中国非物质文化遗产' },
      beforeEnter: requireAuth,
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue'),
      meta: { title: '关于我们 - 中国非物质文化遗产' },
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/auth/LoginView.vue'),
      meta: { title: '用户登录 - 中国非物质文化遗产' },
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/auth/RegisterView.vue'),
      meta: { title: '用户注册 - 中国非物质文化遗产' },
    },
    {
      path: '/admin-login',
      name: 'admin-login',
      component: () => import('../views/auth/AdminLoginView.vue'),
      meta: { title: '管理员登录 - 中国非物质文化遗产' },
    },
    {
      path: '/admin',
      name: 'admin',
      component: () => import('../views/admin/AdminView.vue'),
      meta: { title: '管理后台 - 中国非物质文化遗产', requiresAuth: true },
      beforeEnter: requireAuth,
    },
    // 404页面
    {
      path: '/:catchAll(.*)',
      name: 'not-found',
      component: () => import('../views/NotFoundView.vue'),
      meta: { title: '页面未找到 - 中国非物质文化遗产' },
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
})

// 更新页面标题
router.beforeEach((to, from, next) => {
  document.title = to.meta.title || '中国非物质文化遗产'
  next()
})

export default router
